package com.report.pdf;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.Pipeline;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.AbstractImageProvider;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

public class XMLWorkerDefault {
	public static void main(String[] args) {
		defaultExample();
	}

	private static void defaultExample() {
		
		// create a new document
		Document document = new Document();
		try {
			
			String htmlReportFile = "html/report.html";
			String pdfFile = "results/report.pdf";
			
			buildHtml("html/report-template.html", htmlReportFile);
			System.out.println("Generating PDF file...");
			// create a pdfwriter instance
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream(pdfFile));
			// open the document
			document.setPageSize(PageSize.A4.rotate());
			document.setMargins(15,10,10,10);
			document.open();

			HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
			htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
			htmlContext.setImageProvider(new AbstractImageProvider() {
				public String getImageRootPath() {
					return "html/";
				}
			});

			CSSResolver cssResolver = XMLWorkerHelper.getInstance()
					.getDefaultCssResolver(true);

			Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
					new HtmlPipeline(htmlContext, new PdfWriterPipeline(
							document, writer)));
			XMLWorker worker = new XMLWorker(pipeline, true);
			XMLParser p = new XMLParser(worker);
			p.parse(new FileInputStream(htmlReportFile));
			
			System.out.println(String.format("Finished! %s is ready to use!", pdfFile));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		document.close();
	}

	public static void buildHtml(String inputPath,String outputPath) throws IOException {
		System.out.println("Preparing the content...");
		Velocity.init();
		Template t = Velocity.getTemplate(inputPath);

		VelocityContext ctx = new VelocityContext();
		List<Statement> statements = new ArrayList<Statement>();
		statements.add(new Statement("KKhang"));
		statements.add(new Statement("KKhang1"));
		statements.add(new Statement("KKhang2"));

		ctx.put("statements", statements);
		Writer writer = new StringWriter();
		t.merge(ctx, writer);
		
		FileWriter fw = new FileWriter(outputPath);
		fw.write(writer.toString());
		fw.flush();
		fw.close();
		System.out.println("Content is ready!");
	}
}
